package cn.itcast.service.system.impl;

import cn.hutool.core.util.IdUtil;
import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;

import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void save(Role role) {
        // 设置主键
        role.setId(IdUtil.simpleUUID());
        roleDao.save(role);
    }

    @Override
    public void update(Role role) {
        roleDao.update(role);
    }

    @Override
    public void delete(String id) {

        roleDao.delete(id);
    }

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize,String companyId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo(list,5);
    }

    @Override
    public List<String> findModuleIdsByRoleId(String roleId) {
        return roleDao.findModuleIdsByRoleId(roleId);
    }

    @Override
    public void updateRoleModule(String roleId, String[] moduleIds) {
        roleDao.deleteModuleIdsByRoleId(roleId);

        if(moduleIds!=null && moduleIds.length>0){
            for (String moduleId : moduleIds) {
                roleDao.saveRoleIdAndModuleId(roleId,moduleId);
            }
        }
    }
}
